<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use think\Db;
use app\common\model\MessageLogs;
use app\common\model\Attachment;
/**
 * 微信用户管理
 *
 * @icon   fa fa-list
 * @remark 用于管理网站的所有分类,分类可进行无限级分类,分类类型请在常规管理->系统配置->字典配置中添加
 */
class Mlogs extends Backend
{
	protected $noNeedLogin = [''];
    protected $noNeedRight = [''];

    public function _initialize()
    {
        parent::_initialize();
        //移除HTML标签
        $this->model = model('app\common\model\MessageLogs');
    }
    public function index(){
    	if ($this->request->isAjax()) {
    		$map=[];
    		$map['wechat_id'] = $this->wechat_id;
            $map['is_reply'] = 0;
    		list($where, $sort, $order, $offset, $limit) = $this->buildparams('content');
    		$list = $this->model->with('member')
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->paginate($limit);
			$result = array("total" => $list->total(), "rows" => $list->items());
			return json($result);
    	}
    	return $this->view->fetch();
    }
    public function send(){
        if($this->request->isPost()){
            $data = $this->request->post('row/a');
            if(empty($data['ids'])){
                $this->error('缺少接收用戶');
            }
            $mess = [];
            // 文字回复
            if($data['type'] == 'text'){
                if(empty($data['content'])){
                    $this->error('请输入回复内容');
                }
                $mess = [
                    'msgtype' => 'text',
                    'text' => ['content'=>$data['content']],
                ]; 
            }
            // 图片回复
            if($data['type'] == 'image'){
                if(empty($data['image'])){
                    $this->error('请上传回复图片');
                }
                if(empty($data['media_id'])){
                    $data['media_id'] = (new Attachment)->getMedia_id($data['image'],$this->wechat_id);
                }
                $mess = [
                    'msgtype' => 'image',
                    'image' => ['media_id'=>$data['media_id']],
                ]; 
            }
            
            $ids = explode(",", $data['ids']);
            $weobj = $this->weobj();
            $openids = [];
            foreach($ids as $id){
                $openid = $this->model->where('id',intval($ids))->value('openid');
                if(empty($openid)){
                    continue;
                }
                Db::startTrans();
                try{
                    $this->model->insert(
                        array(
                            'wechat_id' => $this->wechat_id,
                            'openid' => $openid,
                            'msg_type' => $data['type'],
                            'content' => $data['content'],
                            'is_reply' => 1,
                            'create_time' => time(),
                            'log_id' => $id,
                            'url'  => $data['image'],
                            'media_id' => $data['media_id'],
                        )
                    );
                    if(!in_array($openid, $openids)){
                        $openids[] = $openid;
                        $mess['touser'] = $openid;
                        $result = $weobj->sendCustomMessage($mess);
                        if($result === false){
                            Db::rollback();
                            continue;
                        }
                    }
                }catch(\think\Exception $e){
                    Db::rollback();
                }
                Db::commit();
            }
            $this->success('消息已经发送成功');
        }
        return $this->view->fetch();
    }
    
}